package edu.uic.keyoutcomestrackertool;

import java.util.List;

import edu.uic.kott.db.KottDataSource;
import edu.uic.kott.model.Course;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class Courses extends ListActivity {

	private static final String LOGTAG = "KOTT";

	private static final int REQUEST_CODE = 104;

	KottDataSource datasource; 

	List<Course> allCourses;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.view_courses);

		datasource = new KottDataSource(this);

		refreshDisplay();
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);

		Course course = allCourses.get(position);
		Toast msg = Toast.makeText(this, "Testing!", Toast.LENGTH_SHORT);
		msg.show();		

		Intent intent = new Intent(this, CourseDetails.class);
		intent.putExtra("courseId", String.valueOf(course.getCourseId()));
		startActivityForResult(intent, REQUEST_CODE);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.menu_add, menu);		
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Intent intent = new Intent(this, AddEditCourse.class);
		startActivityForResult(intent, REQUEST_CODE);

		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent intent) {		
		super.onActivityResult(requestCode, resultCode, intent);

		if(requestCode == REQUEST_CODE && resultCode == RESULT_OK){

			Log.i(LOGTAG,"1 Option is : " + intent.getStringExtra("option"));

			Log.i(LOGTAG,"2 Course id is " + String.valueOf(intent.getStringExtra("id")));

			if("add".equalsIgnoreCase(intent.getStringExtra("option"))){

				Course course = new Course();
				course.setCourseName(intent.getStringExtra("className"));
				course.setStartDate(intent.getStringExtra("startDate"));
				course.setEndDate(intent.getStringExtra("endDate"));
				course.setClassDays(intent.getStringExtra("days"));
				course.setStartTime(intent.getStringExtra("startTime"));
				course.setEndTime(intent.getStringExtra("endTime"));
				course.setTargetedWSS(intent.getStringExtra("tWSS"));
				course.setClassLocation(intent.getStringExtra("classLocation"));
				course.setInstructor(intent.getStringExtra("instructorId"));
				course.setTa(intent.getStringExtra("taId"));

				long courseId = datasource.createCourse(course);
				Log.i(LOGTAG,"Course " + course.getCourseName() + " created with id " + courseId);
			}
			refreshDisplay();
		}
	}

	public void refreshDisplay(){
		allCourses = datasource.findAllCourses();

		if(allCourses.size() > 0){	
			ArrayAdapter<Course> allCoursesAdapter = new ArrayAdapter<Course>(this, android.R.layout.simple_list_item_1, allCourses);
			setListAdapter(allCoursesAdapter);
		}
		else{
			Log.i(LOGTAG, "No courses added!");
		}
	}
}
